//
//  MyServiceVC.m
//  MyProject
//
//  Created by jiaming yan on 2018/3/22.
//  Copyright © 2018年 yanjiaming. All rights reserved.
//

#import "MyServiceVC.h"
#import "AddDecryptVC.h"
#import "AddEnsureVC.h"
#import "ServiceListTableViewCell.h"
#import "ServiceTypeHead.h"
#import "ServiceTypeFooter.h"
#import "MyServiceAnswersTableViewCell.h"
#import "NSUDManager.h"
#import "EnsureListModel.h"
#import "DecrypListModel.h"
#import "AnswersListModel.h"
#import "AddSkillsVC.h"
#import "HelpViewController.h"
#import "WebViewController.h"

@interface MyServiceVC ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic)  NSMutableArray *arrData;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topViewHeight;
@property (strong, nonatomic)  NSMutableArray *arrEnsureData;
@property (strong, nonatomic)  NSMutableArray *arrDecryptData;
@property (strong, nonatomic)  NSMutableArray *arrAnswersData;
@end

@implementation MyServiceVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"我的服务";
    [self loadTabbleView];
    [self requestAnswers];
    [self requestDecrypt];
    [self requestEnsure];
    [self rightButton];
    
}
-(void)rightButton{
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 0, 30, 44);
    //    [backBtn setTitle:@"保存" forState:UIControlStateNormal];
    [backBtn setImage:IMAGE(@"ic_collect_help") forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(help) forControlEvents:UIControlEventTouchUpInside];
    //    backBtn.backgroundColor = kGreenColor;
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    
    
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
                                       initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                       target:nil action:nil];
    negativeSpacer.width = -20;
    self.navigationItem.rightBarButtonItems = @[negativeSpacer,backItem];
    
    if (IOS11Later) {
        backBtn.contentHorizontalAlignment =UIControlContentHorizontalAlignmentCenter;
    }
}
-(void)help{
    HelpViewController *vc = [[HelpViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self requestAnswers];
    [self requestDecrypt];
    [self requestEnsure];
}
- (IBAction)closeClick:(id)sender {
    self.topViewHeight.constant = 0;
}
/*
 方法描述:
 问答服务
 
 参数说明:
 <#参数说明#>
 
 返回结果:
 <#返回结果#>
 
 */
-(void)requestAnswers{
    UserModel *user = [NSUDManager currentUser];
    [YJMSendRequest postDataWithParameters:@{@"pageSize":@"1000",@"pageNo":@"1",@"userId":user.userId} url:@"question/getIncumbentQuestions" result:^(NSDictionary *result, NSError *error) {
        result = [CommonUtils decryptDicWithResult:result];
        [self hideProgress:self.view animated:YES];
        if([result[@"result"] integerValue] == 1)
        {
            self.arrAnswersData = [AnswersListModel mj_objectArrayWithKeyValuesArray:result[@"data"]];
            [self.tableView reloadData];
        }else{
            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
    }];
    
}
/*
 方法描述:
 解密服务
 
 参数说明:
 <#参数说明#>
 
 返回结果:
 <#返回结果#>
 
 */
-(void)requestDecrypt{
    UserModel *user = [NSUDManager currentUser];
    [YJMSendRequest postDataWithParameters:@{@"pageSize":@"1000",@"pageNo":@"1",@"userId":user.userId} url:@"decrypt/getIncumbentDecrypt" result:^(NSDictionary *result, NSError *error) {
        result = [CommonUtils decryptDicWithResult:result];
        [self hideProgress:self.view animated:YES];
        if([result[@"result"] integerValue] == 1)
        {
            self.arrDecryptData = [DecrypListModel mj_objectArrayWithKeyValuesArray:result[@"data"][@"list"]];
            [self.tableView reloadData];
        }else{
            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
    }];
    
}
/*
 方法描述:
 保过服务
 
 参数说明:
 <#参数说明#>
 
 返回结果:
 <#返回结果#>
 
 */
-(void)requestEnsure{
    UserModel *user = [NSUDManager currentUser];
    [YJMSendRequest postDataWithParameters:@{@"pageSize":@"1000",@"pageNo":@"1",@"userId":user.userId} url:@"assurePass/getIncumbentAssurePass" result:^(NSDictionary *result, NSError *error) {
        result = [CommonUtils decryptDicWithResult:result];
        [self hideProgress:self.view animated:YES];
        if([result[@"result"] integerValue] == 1)
        {
            self.arrEnsureData = [EnsureListModel mj_objectArrayWithKeyValuesArray:result[@"data"][@"list"]];
            [self.tableView reloadData];
        }else{
            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
    }];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)addAnswerClick:(id)sender {
    AddSkillsVC *vc = [[AddSkillsVC alloc]init];
    vc.skillsArr = self.arrAnswersData;
    [self.navigationController pushViewController:vc animated:YES];
}
- (IBAction)addDecryptClick:(id)sender {
    AddDecryptVC *vc = [[AddDecryptVC alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}
- (IBAction)addEnsureClick:(id)sender {
    
    AddEnsureVC *vc = [[AddEnsureVC alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}
/*
 方法描述:
 加载列表
 
 参数说明:
 <#参数说明#>
 
 返回结果:
 <#返回结果#>
 
 */
-(void)loadTabbleView{
    
    [self.tableView registerNib:[UINib nibWithNibName:@"MyServiceAnswersTableViewCell" bundle:nil] forCellReuseIdentifier:@"MyServiceAnswersTableViewCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"ServiceListTableViewCell" bundle:nil] forCellReuseIdentifier:@"ServiceListTableViewCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"ServiceTypeHead" bundle:nil] forHeaderFooterViewReuseIdentifier:@"ServiceTypeHead"];
    [self.tableView registerNib:[UINib nibWithNibName:@"ServiceTypeFooter" bundle:nil] forHeaderFooterViewReuseIdentifier:@"ServiceTypeFooter"];
    
    self.tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 40)];
    self.tableView.estimatedRowHeight = 184.0f;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        // Fallback on earlier versions
    }
}
#pragma mark ------tableView代理
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    static NSString *identifier = @"ServiceTypeHead";
    ServiceTypeHead *headView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:identifier];
    headView.rightView.hidden = YES;
    if (section == 0) {
        [headView.helpButton addTarget:self action:@selector(helpClick) forControlEvents:UIControlEventTouchUpInside];
        headView.typeLabel.text = @"问答服务（最多6个）";
        headView.typeImageView.image = IMAGE(@"ic_wendafuwu1");
        headView.rightView.hidden = NO;
    }else if (section == 1) {
        headView.typeLabel.text = @"答疑解密服务";
        headView.typeImageView.image = IMAGE(@"ic_wemzhang");
    }else if (section == 2) {
        headView.typeLabel.text = @"入职保过服务";
        headView.typeImageView.image = IMAGE(@"ic_baozhang");
    }
    return headView;
}
-(void)helpClick{
    WebViewController *vc = [[WebViewController alloc]init];
    vc.titleStr =@"用户协议";
    vc.url = [NSString stringWithFormat:@"%@%@",ImageBaseURL,@"新手服务指南.docx"];
    vc.isWord = YES;
    [self.navigationController pushViewController:vc animated:YES];
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    static NSString *identifier = @"ServiceTypeFooter";
    ServiceTypeFooter *footer = [tableView dequeueReusableHeaderFooterViewWithIdentifier:identifier];
    [footer.addButton removeTarget:self action:@selector(addDecryptClick:) forControlEvents:UIControlEventTouchUpInside];
    [footer.addButton removeTarget:self action:@selector(addEnsureClick:) forControlEvents:UIControlEventTouchUpInside];
    if (section == 0) {
        [footer.addButton setTitle:@"添加擅长技能" forState:UIControlStateNormal];
         [footer.addButton addTarget:self action:@selector(addAnswerClick:) forControlEvents:UIControlEventTouchUpInside];
        
    }else if (section == 1) {
        [footer.addButton setTitle:@"添加解密专题" forState:UIControlStateNormal];
        [footer.addButton addTarget:self action:@selector(addDecryptClick:) forControlEvents:UIControlEventTouchUpInside];
        
    }else if (section == 2) {
        [footer.addButton setTitle:@"添加入职保过" forState:UIControlStateNormal];
        [footer.addButton addTarget:self action:@selector(addEnsureClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return footer;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 34.0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 35;
}

//- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
//    
//}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0 ) {
        return 1;
    }else if (section == 1 ) {
        return self.arrDecryptData.count;
    }
    else if (section == 2) {
        return self.arrEnsureData.count;
    }
    return 0;
}

//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    return 65;
//}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        static NSString *identifier = @"MyServiceAnswersTableViewCell";
        MyServiceAnswersTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        cell.selectionStyle =  UITableViewCellSelectionStyleNone;
        cell.contentArr = self.arrAnswersData;
        return cell;
    }else if (indexPath.section == 2) {
        EnsureListModel *model = self.arrEnsureData[indexPath.row];
        static NSString *identifier = @"ServiceListTableViewCell";
        ServiceListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        cell.selectionStyle =  UITableViewCellSelectionStyleNone;
        cell.priceLabel.text = [NSString stringWithFormat:@"¥%.2f",model.price];
        if (model.isEnable) {
            cell.typeImageView.hidden = YES;
        }else{
            cell.typeImageView.hidden = NO;
        }
        cell.titleLabel.text = [NSString stringWithFormat:@"保过职位：%@",model.jobName];
        cell.commentLabel.text = [NSString stringWithFormat:@"%zd条评价",model.evaluateCount];
        cell.starView.value = model.star;
        return cell;
    }else{
        DecrypListModel *model = self.arrDecryptData[indexPath.row];
        static NSString *identifier = @"ServiceListTableViewCell";
        ServiceListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        cell.selectionStyle =  UITableViewCellSelectionStyleNone;
        cell.priceLabel.text = [NSString stringWithFormat:@"¥%.2f",model.price];
        if (model.isEnable) {
            cell.typeImageView.hidden = YES;
        }else{
            cell.typeImageView.hidden = NO;
        }
        cell.titleLabel.text = [NSString stringWithFormat:@"%@",model.title];
        cell.commentLabel.text = [NSString stringWithFormat:@"%zd条评价",model.evaluateCount];
        cell.starView.value = model.star;
        return cell;
    }
   
    
}
- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section
{
    UITableViewHeaderFooterView *footer = (UITableViewHeaderFooterView *)view;
    [footer.textLabel setTextColor:[UIColor whiteColor]];
}

- (void )tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
       
    }else if (indexPath.section == 2) {
        EnsureListModel *model = self.arrEnsureData[indexPath.row];
        AddEnsureVC *vc = [[AddEnsureVC alloc]init];
        vc.model = model;
        [self.navigationController pushViewController:vc animated:YES];
    }else{
        DecrypListModel *model = self.arrDecryptData[indexPath.row];
        AddDecryptVC *vc = [[AddDecryptVC alloc]init];
        vc.model = model;
        [self.navigationController pushViewController:vc animated:YES];
    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
